package com.example.administrator.szb.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.activity.LoginNewActivity_two;
import com.example.administrator.szb.bean.GZBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;
import java.util.Map;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by stone on 2018/1/18.
 */

public class CommonPost {
    public static void doGuanZhu(final Context context, int refer_id, final OnSuccessListener l) {
        if (!(SPUtils.isLogin())) {
            context.startActivity(new Intent(context, LoginNewActivity_two.class));
            return;
        }
        if (refer_id == SPUtils.getUserId()) {
            DialogUtil.showToast(context, "不能关注自己");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        int id = SampleApplicationLike.getUserloginInstance().getId();
        map.put("id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("quiz_id", refer_id + "");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, GZBean.class, URLAddress.URLBASE + URLAddress.URL_GZ, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {

               if (l!=null){
                   l.onSuccess(response);
               }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
                //DialogUtil.showSimpleDialog(HDXQActivity.this, "服务器异常，请重新尝试");
                Toasts.show(context, "发生异常。。。", Toast.LENGTH_SHORT);
            }
        });


    }

    /**
     * (类型 1收藏的项目  2收藏的服务  3收藏的案例)
     * @param context
     * @param type
     * @param other_id
     * @param listener
     */
    public static void doSc(Activity activity, int type, int other_id, final OnShouCangListener listener){
        if (CheckInfoUtils.isLogin(context)){
            Map<String,Object> map = new HashMap<>();
            map.put("type",type);
            map.put("other_id",other_id);
            map.put("uid",SPUtils.getUserId());
            HttpUtils.post(activity,URLAddress.COLLECTS, map, new OnResultListener() {
                @Override
                public void onSuccess(int requestCode, String str) {
                    if (listener!=null){
                        listener.onSuccess(JSON.parseObject(str,new TypeReference<ReslutBean>(){}));
                    }
                }

                @Override
                public void onFailure(int requestCode, String str) {
                    if (listener!=null) {
                        listener.onFail(str);
                    }
                }
            });
        }
    }
    public static void isCollect(Activity activity,int type,int id,OnResultListener listener){
        Map<String,Object> map = new HashMap<>();
        map.put("uid",SPUtils.getUserId());
        map.put("type",type);
        map.put("id",id);
        HttpUtils.post(activity,URLAddress.COLLECTS_GETISCOLLECT,map,listener );
    }
    public static void callPhone(Activity activity,int id,int type){
        //type 0项目详情里拨打  1特色服务里拨打
        Map<String,Object> map = new HashMap<>();
        map.put("uid",SPUtils.getUserId());
        map.put("other_id",id);
        map.put("type",type);
        HttpUtils.post(activity,URLAddress.CALLPHONE,map,null );
    }

    public interface OnShouCangListener{
        void onSuccess(ReslutBean bean);
        void onFail(String str);
    }

    public  interface OnSuccessListener{
        void onSuccess(Object response);
    }
    /*
    * private void showNodata(){
        freshlayout.setVisibility(View.VISIBLE);
        noda_ll.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.GONE);
        QTLog.e("aaaaa");
    }
    private void hideAll(){
        noda_ll.setVisibility(View.GONE);
        freshlayout.setVisibility(View.VISIBLE);
        listView.setVisibility(View.VISIBLE);
        error_net_ll.setVisibility(View.GONE);
    }
    private void showError(){
        freshlayout.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.VISIBLE);
    }
    * */
}

